﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace EthnicEdu.Pages
{
    public partial class Login : PhoneApplicationPage
    {
        public Login()
        {
            InitializeComponent();
            var setting = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
            if (!setting.Contains("IsRemember"))
            {
                setting.Add("IsRemember", false);
                setting.Add("UserName", string.Empty);
                setting.Add("Pwd", string.Empty);
            }
            else
            {

                chkRemember.IsChecked = (bool)setting["IsRemember"];
                txtUserName.Text = setting["UserName"].ToString();
                pwbPwd.Password = setting["Pwd"].ToString();
            }


        }
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(pwbPwd.Password.Trim()) && !string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                App.myService.GetAccountByUserNameAndPassAsync(txtUserName.Text.Trim(), pwbPwd.Password.Trim());
                btnLogin.IsEnabled = false;
                pbLoading.Visibility = System.Windows.Visibility.Visible;
                App.myService.GetAccountByUserNameAndPassCompleted += new EventHandler<Service.GetAccountByUserNameAndPassCompletedEventArgs>(myService_GetAccountByUserNameAndPassCompleted);
            }
            else
            {
            }
        }

        void myService_GetAccountByUserNameAndPassCompleted(object sender, Service.GetAccountByUserNameAndPassCompletedEventArgs e)
        {
            btnLogin.IsEnabled = true;
            pbLoading.Visibility = System.Windows.Visibility.Collapsed;
            if (e.Result == null)
            {
                 MessageBox.Show("Wrong username or password");
            }
            else
            {
                App.AccountID =  e.Result.AccountId ;
                App.JobID =  int.Parse( e.Result.JobId.ToString());
            
                var setting = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;

                setting["IsRemember"] = chkRemember.IsChecked;
                if (chkRemember.IsChecked == true)
                {
                    
                    setting["UserName"] =  txtUserName.Text;
                      setting["Pwd"] = pwbPwd.Password;
                    
                }
                else
                {

                    setting["UserName"] = string.Empty;
                    setting["Pwd"] = string.Empty;
                }
                NavigationService.Navigate(new Uri(@"/MainPage.xaml", UriKind.Relative));

            }
          


        }
    }
}